const app = getApp()
const WXAPI = require("../../../../api/newApi")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    nvabarData: {
      showCapsule: 1, // 是否显示左上角图标   1表示显示    0表示不显示
      title: '预付卡', // 导航栏 中间的标题
      textColor: '#fff', // 标题颜色
      bgColor: '', // 导航栏背景颜色
      isColor: false,
      isScrollY: '',
      isShare: '' //是否来自分享 0 :不是，1：是
    },
    assetId: '',
    info: {},
    shopLs: [], //商家列表
    imgHttp: app.globalData.imgHttp,
    pageSize: 10,
    pageNum: 1,
    cardTotal: 0,
    lnt: '', //用户经度
    lat: '', //用户纬度
    lodingtype: 1, //加载状态（0：加载中  1：加载完成没有更多)
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      statusHeight: app.gettop.height,
      status: app.gettop.status,
      navHeight: app.gettop.navHeight,
      mobbleBottom: app.mobile.mobbleBottom,
    })
    this.data.assetId = options.assetId
    this.getInfo()
    this.getshopLs()
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },
  getInfo() { //获取卡信息
    var data = {
      assetId: this.data.assetId
    }
    WXAPI.wealDrawInfo(data, (res) => {
      if (res.code == 200) {
        this.setData({
          info: res.result
        })
      }
    })
  },
  /*
   * 页面滚动
   **/
  onPageScroll(e) {
    var that = this;
    var scrollY = e.scrollTop
    if (scrollY > 50) {
      that.setData({
        ['nvabarData.textColor']: '#000',
        ['nvabarData.isScrollY']: true,
        ['nvabarData.isColor']: true,
      })
      wx.setNavigationBarColor({
        frontColor: '#000000',
        backgroundColor: '#000000',
      })
    } else if (scrollY < 50) {
      that.setData({
        ['nvabarData.textColor']: '#fff',
        ['nvabarData.isScrollY']: false,
        ['nvabarData.isColor']: false,
      })
      wx.setNavigationBarColor({
        frontColor: '#ffffff',
        backgroundColor: '#ffffff',
      })
    }
  },
  getshopLs() { //获取列表
    wx.showLoading({
      title: '加载中',
    })
    var data = {
      pageNum: this.data.pageNum,
      pageSize: this.data.pageSize,
      assetId: this.data.assetId,
      lnt: this.data.lnt, //用户经度
      lat: this.data.lat, //用户纬度
    }
    WXAPI.assetCardWealShopInfo(data, (res) => {
      const data = res.result
      if (res.code == 200) {
        // this.data.cardtotal = res.result.total
        if (res.result.total == -1) {
          this.data.cardTotal = 0
        } else {
          this.data.cardTotal = res.result.total
        }
        this.setData({
          cardTotal: this.data.cardTotal
        })
        console.log("判断", this.data.pageNum, Math.ceil(this.data.cardTotal / this.data.pageSize), this.data.cardTotal)
        if (this.data.pageNum >= Math.ceil(this.data.cardTotal / this.data.pageSize)) {
          this.setData({
            lodingtype: 1
          })
        } else {
          this.setData({
            lodingtype: 0
          })
        }
        if (this.data.pageNum > 1) {
          this.data.shopLs = this.data.shopLs.concat(res.result.records); //将数据拼接在一起
          this.setData({
            shopLs: this.data.shopLs
          })
        } else {
          this.data.shopLs = res.result.records
          this.setData({
            shopLs: this.data.shopLs
          })
        }
        wx.hideLoading({})
      }
    })
  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log("触底了", this.data.pageNum, Math.ceil(this.data.cardTotal / this.data.pageSize), this.data.cardTotal)
    if (this.data.pageNum >= Math.ceil(this.data.cardTotal / this.data.pageSize)) {
      console.log('没有分页')
      this.setData({
        lodingtype: 1
      })
    } else {
      this.setData({
        lodingtype: 0
      })
      this.data.pageNum++; //每触底一次 page + 1;
      this.getshopLs();
    }
  },
  //跳转店铺
  handleShop(e) {
    var id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '/userPageB/pages/userStore/storesDetails/storesDetails?shopId=' + id,
    })
  },
  /**
   * 用户点击右上角分享
   */
  // onShareAppMessage: function () {

  // }
})